/**
 * All rights Reserved
 *
 * @Title: MongoDataHandler.java
 * @Package com.amigo.online.provider.sync.abs
 * @Description: (用一句话描述该文件做什么)
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月14日 下午6:26:35
 * @version V1.0
 * @Copyright: 2018  All rights reserved.
 */
package com.amigo.online.provider.sync.abs;

import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.ClassNameConvertEnum;
import com.amigo.online.provider.sync.convert.ConvertService;

/**
 * @version: V 1.0.0
 * @Description: （对类进行功能描述） 
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月14日 下午6:26:35  
 *
 */

@Service("mongoDataHandler")
public class MongoDataHandler implements DataHandlerService {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    MongoService mongoService;

    protected static final Logger logger = LoggerFactory.getLogger(MongoDataHandler.class);

    @Override
    public void updateHandle(JSONObject newObject, Set<String> columnSet, ColumnHandleMap hadlerMap) {
        long id = newObject.getLongValue(hadlerMap.getId());
        Query query = new Query().addCriteria(Criteria.where(hadlerMap.getProjectMap().get(hadlerMap.getId())).is(id));
        Update update = new Update();
        boolean updateFlag = false;
        long result = 0;
        Map<String, String> projectMap = hadlerMap.getProjectMap();
        Map<String, String> classMap = hadlerMap.getClassMap();
        for (String temp : columnSet) {
            if (hadlerMap.getColumnList().contains(temp)) {
                String projectColumn = projectMap.get(temp);
                String className = classMap.get(temp);
                if (StringUtils.isEmpty(projectColumn)) {
                    projectColumn = temp;
                }
                if (className == null) {
                    update.set(projectColumn, newObject.get(temp));
                } else {
                    try {
                        Class<?> clazz = Class.forName(ClassNameConvertEnum.getColumnName(className));
                        update.set(projectColumn, newObject.getObject(temp, clazz));
                    } catch (ClassNotFoundException e) {
                        logger.info("更新获取中" + temp + "字段类型失败！");
                    }
                }
                updateFlag = true;
            }
        }
        if (updateFlag == true) {
            result = mongoService.updateObject(query, update, hadlerMap.getHandlerTemplate(), hadlerMap.getCollection());
            if (result == 0) {
                //logger.error("更新mongo表数据失败！更新数据ID: " + id);
                logger.error("数据更新失败,结果集为:{},更新key:{},更新集:{}!", hadlerMap.getCollection(), id, update.toString());
            }
        }

    }

    @Override
    public void insertHandle(JSONObject source, ColumnHandleMap hadlerMap) {
        String insertConvert = hadlerMap.getInsertConvert();
        if (insertConvert != null) {
            @SuppressWarnings({"unchecked", "rawtypes"})
            Document document = (Document) ((ConvertService) applicationContext.getBean(insertConvert)).convert(source, hadlerMap.getTableName());
            mongoService.insertObject(document, hadlerMap.getHandlerTemplate(), hadlerMap.getCollection());
        } else {
            logger.error("mongo对象插入转换类未实现！");
        }
    }
}
